﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data;
using System.Configuration;
/*----------------------------------------------------------------
// Copyright (C) ET
// 版权所有。 
//
// 文件名：XmlDAL

// 文件功能描述：XML菜单操作

// 创建标识：李东峰 20140916

// 修改标识：

// 修改描述：

//----------------------------------------------------------------*/
namespace ET.Monitor.DAL
{
    public class XmlDAL
    {
        object objJudge = null;
        string strSql = "", strErrMsg = "";

        InvokeWebService iw = new InvokeWebService();
        string rtdbRelationAddress = "";
        string rtdbRealTimeAddress = "";
        string rtdbNameSpace = "";

        public XmlDAL()
        {
            rtdbRelationAddress = ConfigurationManager.ConnectionStrings["RelationURL"].ConnectionString;
            rtdbRealTimeAddress = ConfigurationManager.ConnectionStrings["RealTimeURL"].ConnectionString;
            rtdbNameSpace = ConfigurationManager.ConnectionStrings["NameSpace"].ConnectionString;
        }

        /// <summary>
        /// 判断要下载的xml文件在数据库中是否是空记录
        /// </summary>
        /// <returns></returns>
        public bool IsEmptyXml(string fileID)
        {
            string errMsg = "";
            string sql = "select * from T_SYS_MENU where T_XMLID='" + fileID + "'";
            DataTable dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { sql, strErrMsg });

            if (dt.Rows.Count == 0 || dt == null)
            {
                return true;
            }
            else
                return false;
        }

        /// <summary>
        /// 从数据库下载最新xml菜单
        /// </summary>
        /// <param name="fileID">xml菜单的名字</param>
        /// <param name="filePath">存放xml的位置</param>
        /// <returns></returns>
        public bool DownLoadXml(string fileID, string filePath)
        {
            
            return (bool)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "DownLoadXml", new object[] { fileID, filePath });
        }

        public DataTable GetXmlFile(string fileID)
        {
            DataTable dt = new DataTable();
            string errMsg = "";
            string strSql = "select * from T_SYS_MENU where T_XMLID='" + fileID + "'";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });

            return dt;
        }

        public bool UpLoadXml(string fileId, byte[] btXml)
        {
            DataTable dt = new DataTable();
            bool judge = false;
            string errMsg = "";
            string sql1 = "select * from T_SYS_MENU where T_XMLID='" + fileId + "'";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { sql1, strErrMsg });


            if (dt != null && dt.Rows.Count != 0)
            {
                strSql = "delete from T_SYS_MENU where T_XMLID='" + fileId + "'";
                iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunNonQuery", new object[] { strSql, strErrMsg });
            }
            //IList<Hashtable> list = new List<Hashtable>();
            //Hashtable ht = new Hashtable(); ht.Add("key", "T_XMLID"); ht.Add("value", fileId); list.Add(ht);
            //ht = new Hashtable(); ht.Add("key", "T_XMLNAME"); ht.Add("value", "WebMenu"); list.Add(ht);

            DataTable dtxml = new DataTable();
            DataColumn dc = new DataColumn("key", Type.GetType("System.String"));
            dtxml.Columns.Add(dc);
            dc =new DataColumn("value", Type.GetType("System.String"));
            dtxml.Columns.Add(dc);
            DataRow dr = dtxml.NewRow();
            dr["key"] = "T_XMLID";
            dr["value"] = fileId;
            dtxml.Rows.Add(dr);

            dr = dtxml.NewRow();
            dr["key"] = "T_XMLNAME";
            dr["value"] = "WebMenu";
            dtxml.Rows.Add(dr);
            DataSet ds = new DataSet();
            ds.Tables.Add(dtxml);

            judge = (bool)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "UpDocument", new object[] { ds, "T_SYS_MENU", "B_XML", btXml });
            return judge;
        }

        #region 添加菜单信息
        public bool AddPage(string id, int order, string icon, string name, string link)
        {
            strSql = "insert into T_SYS_MEMBERLINK(T_USERID,T_MENULINK,T_MENUNAME,T_ICONLINK,I_ORDER) values ('" + id + "','" + link + "','" + name + "','" + icon + "','" + order + "')";
            return (bool)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunNonQuery", new object[] { strSql, strErrMsg });
        }
        #endregion

        #region 加载 Page
        public DataTable GetPage(string uid)
        {
            strSql = "select T_MENULINK,T_MENUNAME,T_ICONLINK,I_ORDER from T_SYS_MEMBERLINK where T_USERID='" + uid + "'";

            return (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql,strErrMsg });
        }
        #endregion

        #region 删除 page
        public bool DelPage(string uid, string order)
        {
            strSql = "delete from T_SYS_MEMBERLINK where T_USERID='" + uid + "' and I_ORDER='" + order + "'";
            return (bool)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunNonQuery", new object[] { strSql, strErrMsg });
        }
        #endregion

        #region
        public object CheckLink(string link, string userID)
        {
            object obj = new object();
            strSql = "select count(*) from T_SYS_MEMBERLINK where T_USERID='" + userID + "' and T_MENULINK='" + link + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            return obj;
        }
        #endregion
    }
}
